<template>
    <div>
       <el-row>
  <el-col :span="8" :xs="2"><div class="grid-content bg-purple"></div></el-col>
  <el-col :span="8" :xs="20"><div class="grid-content bg-purple-light">
      <div style="margin: 20px;"></div>
      <el-card class="box-card">
<el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm" :label-position="labelPosition" >
  <el-form-item label="账号" prop="account">
    <el-input type="text" v-model="ruleForm2.account" placeholder="请输入账号"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="Pass">
    <el-input type="password" v-model="ruleForm2.pass" auto-complete="off" placeholder="请输入密码"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="checkPass">
    <el-input v-model="ruleForm2.checkPass" type="password" auto-complete="off" placeholder="请确认密码"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
    <el-button @click="resetForm('ruleForm2')">重置</el-button>
  </el-form-item>
</el-form>
      </el-card>
      </div></el-col>
  <el-col :span="8" :xs="2"><div class="grid-content bg-purple"></div></el-col>
</el-row> 
    </div>
</template>
<script>
 export default {
    data() {
      var account = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('账号不能为空'));
        }
        callback();
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm2.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        labelPosition: 'top',
        ruleForm2: {
          pass: '',
          checkPass: '',
          account: ''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          account: [
            { validator: account, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {

        var c = this;

     this.$http.post('/api/user/login',{username:c.ruleForm2.account,password:c.ruleForm2.pass}).then(
       (res)=>{
          
          if(res.data.erron != 0){

           alert("输入账号不存在")
         
           return ;

          }

 this.$store.dispatch("setusername",c.ruleForm2.account)
 this.$store.dispatch("userLogin", true);


 this.$router.push({ path: 'main' }) 


       },(res) =>{
        console.log(res.status)
       }
     )


       
  
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    beforeMount(){

let c =this;
      this.$store.dispatch("getusername");
    
  

     console.log(this.$store.getters.isLogin)

if(this.$store.getters.isLogin){
   c.$router.push({ path: 'main' })
}
        
     
   

    }
  }
</script>
<style>

  .el-col {
    border-radius: 4px;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  .button{
      width:100%;
      margin-top: 20px;
  }
</style>